﻿Imports System.Text

Public Module ModuleText

    Public Function ConvertToString(ByVal data() As Byte) As String
        Return Encoding.GetEncoding("gb2312").GetString(data, 0, data.Length)
    End Function

    Public Function ConvertToByteArray(ByVal str As String) As Byte()
        Return Encoding.GetEncoding("gb2312").GetBytes(str)
    End Function

    Public Function MatchRegx(s As String, p As String) As Boolean
        Dim regx As New System.Text.RegularExpressions.Regex(p)
        Dim r = regx.Match(s)
        Return r.Success
    End Function

    Public Function GetLeft(ByVal Source As String, ByVal Filter As String) As String
        If Source.Contains(Filter) = False Then
            Return ""
        Else
            Return Source.Substring(0, Source.IndexOf(Filter))
        End If
    End Function

    Public Function GetRight(ByVal Source As String, ByVal Filter As String) As String
        If Source.Contains(Filter) = False Then
            Return ""
        Else
            Return Source.Substring(Source.IndexOf(Filter) + Filter.Length)
        End If
    End Function

    Public Function SplitLines(ByVal s As String, Optional ByVal op As StringSplitOptions = StringSplitOptions.RemoveEmptyEntries) As String()
        Return s.Split({Environment.NewLine}, op)
    End Function

#If WINDOWS Then

    ''' <summary>
    ''' 取得一个文本文件的编码方式。如果无法在文件头部找到有效的前导符，encoding.default将被返回.
    ''' </summary>
    ''' <param name="filename"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetEncoding(ByVal filename As String) As Encoding
        Return GetEncoding(filename, Encoding.Default)
    End Function

    ''' <summary>
    ''' 取得一个文本文件流的编码方式
    ''' </summary>
    ''' <param name="stream"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetEncoding(ByVal stream As FileStream) As Encoding
        Return GetEncoding(stream, Encoding.Default)
    End Function

    ''' <summary>
    ''' 取得一个文本文件的编码方式
    ''' </summary>
    ''' <param name="filename"></param>
    ''' <param name="defaultencoding"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetEncoding(ByVal filename As String, ByVal defaultencoding As Encoding) As Encoding
        Using fs As FileStream = New FileStream(filename, FileMode.Open)
            Dim targetencoding As Encoding = GetEncoding(fs, defaultencoding)
            fs.Close()
            Return targetencoding
        End Using
    End Function

    ''' <summary>
    ''' 取得一个文本文件流的编码方式
    ''' </summary>
    ''' <param name="stream"></param>
    ''' <param name="defaultencoding"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetEncoding(ByVal stream As FileStream, ByVal defaultencoding As Encoding) As Encoding
        Dim targetencoding As Encoding = defaultencoding
        If stream IsNot Nothing AndAlso stream.Length >= 2 Then
            '保存文件流的前4个字节 
            Dim byte1 As Byte = 0
            Dim byte2 As Byte = 0
            Dim byte3 As Byte = 0
            Dim byte4 As Byte = 0
            '保存当前seek位置 
            Dim origpos As Long = stream.Seek(0, SeekOrigin.Begin)
            stream.Seek(0, SeekOrigin.Begin)

            Dim nbyte As Integer = stream.ReadByte()
            byte1 = Convert.ToByte(nbyte)
            byte2 = Convert.ToByte(stream.ReadByte())
            If stream.Length >= 3 Then
                byte3 = Convert.ToByte(stream.ReadByte())
            End If
            If stream.Length >= 4 Then
                byte4 = Convert.ToByte(stream.ReadByte())
            End If
            '根据文件流的前4个字节判断encoding 
            'unicode {0xff, 0xfe}; 
            'be-unicode {0xfe, 0xff}; 
            'utf8 = {0xef, 0xbb, 0xbf}; 
            If byte1 = &HFE AndAlso byte2 = &HFF Then
                'unicodebe 
                targetencoding = Encoding.BigEndianUnicode
            End If
            If byte1 = &HFF AndAlso byte2 = &HFE AndAlso byte3 <> &HFF Then
                'unicode 
                targetencoding = Encoding.Unicode
            End If
            If byte1 = &HEF AndAlso byte2 = &HBB AndAlso byte3 = &HBF Then
                'utf8 
                targetencoding = Encoding.UTF8
            End If
            '恢复seek位置       
            stream.Seek(origpos, SeekOrigin.Begin)
        End If
        Return targetencoding
    End Function

#End If

End Module